package com.zxy.ziems.server.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxy.ziems.server.monitor.convertor.MonitorConvertor;
import com.zxy.ziems.server.monitor.dao.MonitorGroupDeviceMapper;
import com.zxy.ziems.server.monitor.pojo.dto.MonitorDeviceAddDTO;
import com.zxy.ziems.server.monitor.pojo.entity.MonitorGroupDeviceEntity;
import com.zxy.ziems.server.monitor.pojo.vo.MonitorDeviceOldVO;
import com.zxy.ziems.server.monitor.pojo.vo.MonitorDeviceVO;
import com.zxy.ziems.server.monitor.pojo.vo.MonitorGroupDeviceVO;
import com.zxy.ziems.server.monitor.service.MonitorDeviceService;
import com.zxy.ziems.server.monitor.service.MonitorGroupDeviceService;
import com.zxy.ziems.server.mybatis.core.query.QueryWrapperX;
import com.zxy.ziems.server.mybatis.core.service.ServiceImplX;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Copyright (C),
 * @ClassName: MonitorGroupDeviceServiceImpl
 * @Author: yanzhiwei
 * @Description:
 * @Date: 2024/5/16 17:22
 */
@Service
public class MonitorGroupDeviceServiceImpl extends ServiceImplX<MonitorGroupDeviceMapper, MonitorGroupDeviceEntity>
        implements MonitorGroupDeviceService {
    @Autowired
    private MonitorDeviceService monitorDeviceService;


    /**
     * 获取所有分组监控设备
     *
     * @return
     */
    @Override
    public List<MonitorGroupDeviceVO> getDeviceAll() {
        List<MonitorGroupDeviceEntity> monitorGroupDeviceEntityList = this.list();

        List<MonitorDeviceOldVO> collect = monitorDeviceService.getList();


        Map<String, MonitorDeviceOldVO> deviceMap = collect.stream().collect(Collectors.toMap(m -> m.getId(), m -> m));


        List<MonitorGroupDeviceVO> monitorGroupDeviceVOList = monitorGroupDeviceEntityList.stream().map(m -> {
            MonitorDeviceOldVO monitorDeviceVO = deviceMap.get(m.getDeviceId());

            MonitorGroupDeviceVO monitorGroupDeviceVO = MonitorConvertor.INSTANCE.monitorGroupDeviceDOConvert2VO(m, monitorDeviceVO);
            return monitorGroupDeviceVO;
        }).collect(Collectors.toList());

        return monitorGroupDeviceVOList;
    }


    /**
     * 移除原来组数据
     *
     * @param groupId
     */
    private void removeDeviceByGroupId(String groupId) {
        QueryWrapper<MonitorGroupDeviceEntity> queryWrapper = new QueryWrapperX<>();
        queryWrapper.lambda().eq(MonitorGroupDeviceEntity::getGroupId, groupId);
        this.remove(queryWrapper);
    }


    @Override
    public boolean updateGroupDevice(MonitorDeviceAddDTO monitorDeviceAddDTO) {
        Map<String, String> deviceByGroupMap = this.getDeviceByGroupId(monitorDeviceAddDTO.getGroupId());

        // 移除原来分组摄像头
        this.removeDeviceByGroupId(monitorDeviceAddDTO.getGroupId());


        List<MonitorGroupDeviceEntity> monitorDeviceEntityList = new ArrayList<>();
        for (int i = 0; i < monitorDeviceAddDTO.getDeviceIds().size(); i++) {
            MonitorGroupDeviceEntity monitorGroupDeviceEntity = new MonitorGroupDeviceEntity();
            monitorGroupDeviceEntity.setGroupId(monitorDeviceAddDTO.getGroupId());
            String id = monitorDeviceAddDTO.getDeviceIds().get(i);
            String deviceId = deviceByGroupMap.get(id);

            monitorGroupDeviceEntity.setDeviceId(deviceId == null ? id : deviceId);
            monitorGroupDeviceEntity.setSort(i + 1);
            monitorDeviceEntityList.add(monitorGroupDeviceEntity);
        }

        if (monitorDeviceEntityList.isEmpty()) {
            return true;
        }

        return this.saveOrUpdateBatch(monitorDeviceEntityList);
    }

    @Override
    public List<MonitorDeviceOldVO> listNoByGroupId(String groupId) {
        List<MonitorDeviceOldVO> list = monitorDeviceService.getList();

        List<MonitorGroupDeviceEntity> monitorGroupDeviceEntityList = this.getMonitorGroupDeviceEntityList(groupId);
        List<String> deviceIds = monitorGroupDeviceEntityList.stream().map(m -> m.getDeviceId()).collect(Collectors.toList());

        return list.stream().filter(m -> !deviceIds.contains(m.getId())).collect(Collectors.toList());
    }

    /**
     * 获取关系id
     *
     * @param groupId
     * @return
     */
    private Map<String, String> getDeviceByGroupId(String groupId) {
        List<MonitorGroupDeviceEntity> list = getMonitorGroupDeviceEntityList(groupId);

        Map<String, String> collect = list.stream().collect(Collectors.toMap(m -> m.getId(), m -> m.getDeviceId()));
        return collect;
    }

    @Override
    public List<MonitorGroupDeviceEntity> getMonitorGroupDeviceEntityList(String groupId) {
        QueryWrapper<MonitorGroupDeviceEntity> queryWrapper = new QueryWrapperX<>();
        queryWrapper.lambda().eq(MonitorGroupDeviceEntity::getGroupId, groupId);
        List<MonitorGroupDeviceEntity> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public List<MonitorDeviceVO> getList(String monitorGroupId) {

        List<MonitorGroupDeviceEntity> monitorGroupDeviceEntityList = this.getMonitorGroupDeviceEntityList(monitorGroupId);


        final List<MonitorDeviceOldVO> monitorDeviceOldVOList = monitorDeviceService.getList();

        Map<String, MonitorDeviceOldVO> stringMonitorDeviceEntityMap = monitorDeviceOldVOList.stream().collect(Collectors.toMap(m -> m.getId(), m -> m));


        List<MonitorDeviceVO> voList = monitorGroupDeviceEntityList.stream().map(m -> {
            final MonitorDeviceOldVO monitorDeviceEntity = stringMonitorDeviceEntityMap.get(m.getDeviceId());
            MonitorDeviceVO vo = MonitorConvertor.INSTANCE.monitorDeviceDOListConvert2VO(m, monitorDeviceEntity);
            return vo;
        }).collect(Collectors.toList());


        return voList;
    }

    @Override
    public void removeByGroupId(String monitorGroupId) {
        QueryWrapper<MonitorGroupDeviceEntity> queryWrapper = new QueryWrapperX<>();
        queryWrapper.lambda().eq(MonitorGroupDeviceEntity::getGroupId, monitorGroupId);
        this.remove(queryWrapper);

        // 临时处理分组删除，设备未删除问题
        this.removeById(monitorGroupId);
    }

    @Override
    public Long countByDeviceId(String deviceId) {
        QueryWrapper<MonitorGroupDeviceEntity> queryWrapper = new QueryWrapperX<>();
        queryWrapper.lambda().eq(MonitorGroupDeviceEntity::getDeviceId, deviceId);
        return this.count(queryWrapper);
    }

}
